<template>
    <header>
        <van-nav-bar title="登录" left-text="返回" left-arrow @click-left="$router.back()" />
    </header>
    <div class="content">
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="手机号" placeholder="请输入手机号"
                    :rules="[{ required: true, message: '请填写手机号' }, { pattern: /^1(3|4|5|7|8)\d{9}$/, message: '请输入正确的手机号' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="请输入密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>

            <van-row justify="space-between" style="text-align:center">
                <van-col span="12">忘记密码</van-col>
                <van-col span="12" @click="$router.push('/register')">快速注册</van-col>
            </van-row>
        </van-form>
    </div>
</template>

<!-- <script>
import request from '@/utils/request'
import { showSuccessToast, showFailToast } from 'vant'
export default {
    data() {
        return {
            username: '',
            password: ''
        }
    },
    methods: {
        onSubmit(values) {
            console.log(values)
            request.post('/login', { ...values }).then(res => {
                if (res.data.code == 200) {
                    // 登录成功
                    showSuccessToast('登录成功')
                    // 存储token
                    localStorage.setItem('token', res.data.token)
                    // 跳转到首页
                    this.$router.push('/')
                } else {
                    // 登录失败
                    showFailToast(res.data.message)
                }
            })
        }
    }
}
</script> -->
<script>
import request from '@/utils/request'
import { showSuccessToast, showFailToast } from 'vant'
import { ref } from 'vue';
import { useRouter } from 'vue-router'
export default {
    setup() {
        const username = ref('')
        const password = ref('')
        const router = useRouter()
        const onSubmit = (values) => {
            request.post('/login', values).then(res => {
                if (res.data.code == 200) {
                    showSuccessToast('登录成功')
                    // 存储token
                    localStorage.setItem('token', res.data.token)
                    // 跳转到首页
                    router.push('/')
                } else {
                    showFailToast(res.data.message)
                }
            })
        }
        return {
            username,
            password,
            onSubmit,
        };
    }
}
</script>